using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Ltf.MonitorLizard.Forms;

namespace Ltf.MonitorLizard
{
				interface BusyForm
	{
		void Close();
	}

	class Busy : IDisposable
	{
		   private BusyForm indicator;
		private EventHandler incrementHandler;

		public EventHandler IncrementHandler
		{
			get { return incrementHandler; }
		}

		public Busy(string title, string text, bool allowCancel)
		{
			ParameterizedThreadStart pts = new ParameterizedThreadStart(ShowPlainBusy);
			pts.Invoke(new BusyParams(title, text, allowCancel));

		}
		public Busy(string title, string text, int maxProgress, int progressIncrement, bool allowCancel)
		{
			ParameterizedThreadStart pts = new ParameterizedThreadStart(ShowProgressBusy);
			pts.Invoke(new BusyParams(title, text, maxProgress, progressIncrement, allowCancel));

		}

		public void ShowPlainBusy(object args)
		{
			BusyParams bp = (BusyParams)args;
			indicator = new BusyIndicator(bp.Title, bp.Text, bp.AllowCancel);
		}

		public void ShowProgressBusy(object args)
		{
			BusyParams bp = (BusyParams)args;
			ProgressIndicator pi = new ProgressIndicator(bp.Title, bp.Text,bp.MaxProgress, bp.ProgressIncrement, bp.AllowCancel);
			incrementHandler = pi.IncrementHandler;
			indicator = pi;
		}

		#region IDisposable Members

		public void Dispose()
		{
			indicator.Close();
		}

		#endregion
	}

	class BusyParams
	{
		public string Title, Text;
		public int MaxProgress, ProgressIncrement;
		public bool AllowCancel;

		public BusyParams(string title, string text, bool allowCancel)
			: this (title, text, 0,0, allowCancel)
		{
			
		}

		public BusyParams(string title, string text, int maxProgress, int progressIncrement, bool allowCancel)
		{
			Title = title;
			Text = text;
			MaxProgress = maxProgress;
			ProgressIncrement = progressIncrement;
			AllowCancel = allowCancel;
		}

	}
}
